// pages/fillInfo/fillInfo.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    formData: {
      contactName: '',
      phoneNum: ''
    },
    optIndex: null,
    addressData: [],
    coordinateType: 1,
    navigationSetting: {
      title: '填写收件人信息',
      height: app.globalData.navBarHeight,
      paddingTop: app.globalData.statusBarHeight,
      backgroundColor: 'white',
      size: 'default',
      back: '<'
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let _this = this
    app.user.getUserInfo().then(res => {
      console.log(3232323, res)
      if (res.code === 200) {
        _this.setData({ 'formData.contactName': res.data.receiveName })
        _this.setData({ 'formData.phoneNum': res.data.receivePhone })
      }
    })
  },
  submit() {
    if (this.data.formData.contacts === '') {
      wx.showToast({
        title: '联系人不能为空',
        icon: 'none',
        duration: 2000 //持续的时间
      })
      return false
    }

    if (this.data.formData.phoneNum === '') {
      wx.showToast({
        title: '手机号/座机号不能为空',
        icon: 'none',
        duration: 2000 //持续的时间
      })
      return false
    }
    if (this.data.formData.phoneNum.length != 11) {
      wx.showToast({
        title: '手机号必须为11位',
        icon: 'none',
        duration: 2000 //持续的时间
      })
      return false
    }
    app.user.addAddress({
      receivePhone: this.data.formData.phoneNum,
      receiveName: this.data.formData.contactName
    }).then(res => {
      console.log(3232323, res)
      if (res.code === 200) {
        wx.navigateBack()

      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },
  selAddress(e) {
    let optIndex = this.data.optIndex
    if (optIndex === '1') {
      let formData = e.currentTarget.dataset.word
      let endFormData = app.globalData.endFormData
      if (endFormData != undefined) {
        if (formData.longitude == endFormData.longitude && formData.latitude == endFormData.latitude) {
          wx.showToast({
            title: '起点和终点不能相同！',
            icon: 'none'
          })
          return false
        }
      }
      app.globalData.isSuccessSave = true
      app.globalData.formData = formData
    } else if (optIndex === '2') {
      let endFormData = e.currentTarget.dataset.word
      let formData = app.globalData.formData
      if (formData != undefined) {
        if (formData.longitude == endFormData.longitude && formData.latitude == endFormData.latitude) {
          wx.showToast({
            title: '起点和终点不能相同！',
            icon: 'none'
          })
          return false
        }
      }
      app.globalData.endFormData = endFormData
      app.globalData.isEndSuccessSave = true
    }
    let isSuccessSave = app.globalData.isSuccessSave
    let isEndSuccessSave = app.globalData.isEndSuccessSave
    if (isSuccessSave && isEndSuccessSave) {
      wx.navigateTo({
        url: '/pages/order/order',
      })
    } else {
      wx.switchTab({
        url: '/pages/homepage/homepage',
      })
    }
  },
  bindKeyInput: function (e) {
    this.setData({
      'formData.phoneNum': e.detail.value
    })
  },
  bindcontactNameInput: function (e) {
    console.log(33)
    this.setData({
      'formData.contactName': e.detail.value
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})